/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ownPackage;

import java.io.File;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import jxl.*;

/**
 *
 * @author FERMYTH
 */
public class XLSClass {

    public XLSClass(JTextField xls, JTextField res_path, JTextArea Lognya, JTextField out_path) {
        try {
            Workbook workbook = Workbook.getWorkbook(new File(xls.getText()));
            Sheet sheet = workbook.getSheet(0);

            Lognya.append("start . . . ");
            Lognya.append(" \n");

            Cell a1 = sheet.getCell(0, 2);
            Cell a2 = sheet.getCell(1, 2);
            String folder = a1.getContents();
            String oldFolder = a2.getContents();

            System.out.println("a1 = " + folder);
            System.out.println("a2 = " + oldFolder);
            CopyAndMove cm = new CopyAndMove();
            int i = 0;

            File junk = new File("../dist");
            if (junk.exists()) {
                cm.deleteDirectory(junk);
            }

            
            while (!folder.equalsIgnoreCase("done")) {

                File asal = new File(res_path.getText() + "/" + folder);
//                Lognya.append(this.getClass().getResource("../src/res").getPath());
                File tujuan = new File("../src");

                cm.copyDirectory(asal, tujuan);

                //manipulate properties project
                File buildxml = new File("../build.xml");
                FindReplace.replace(buildxml, FindReplace.getContents(buildxml), oldFolder, folder);

                File buildimpl = new File("../nbproject/build-impl.xml");
                FindReplace.replace(buildimpl, FindReplace.getContents(buildimpl), oldFolder, folder);

                File projectxml = new File("../nbproject/project.xml");
                FindReplace.replace(projectxml, FindReplace.getContents(projectxml), oldFolder, folder);

                File properties = new File("../nbproject/project.properties");
                FindReplace.replace(properties, FindReplace.getContents(properties), oldFolder, folder);


                // compiling partthis.getClass().getResource("/src/resources/")
                Process proc = Runtime.getRuntime().exec("execute.bat");
                StreamGobbler outGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", Lognya);

                outGobbler.start();

                proc.waitFor();

                File output_asal = new File("../dist");
                File output_tujuan = new File(out_path.getText() + "/" + folder);

                if (output_tujuan.exists()) {
                    cm.deleteDirectory(output_tujuan);
                }
                
                cm.moveDirectory(output_asal, output_tujuan);

                String Logs = "running on " + folder + " ..... done \n";
                Lognya.append(Logs);

                Lognya.append(" \n");
                Lognya.append("old folder" + oldFolder);
                Lognya.append(" \n");

                i++;
                a1 = sheet.getCell(0, 2 + i);
                a2 = sheet.getCell(1, 2 + i);
                folder = a1.getContents();
                oldFolder = a2.getContents();
            }
            System.out.println("done");
            Lognya.append("done all");

        } catch (Exception ex) {
            System.out.println(ex.toString());
            Lognya.append(ex.toString());
        }
    }
}
